package cn.farm.farm.Service.OridinaryService;

import cn.farm.farm.Entity.Ordinary.Image;
import cn.farm.farm.Mapper.OridinaryMapper.InformationMapper;
import cn.farm.farm.Mapper.OridinaryMapper.OridinaryImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ImageService {

    @Autowired
    OridinaryImageMapper oridinaryImageMapper;

    @Autowired
    InformationMapper informationMapper;

    public void insert(Image image){
        oridinaryImageMapper.insert(image);
    }

    public Image selectByImageID(int imageID){
        return oridinaryImageMapper.selectByImageID(imageID);
    }

    public List<Integer> insertImageList(List<Image> images){
        List<Integer> list = new ArrayList<>();
        Iterator<Image> iterator = images.iterator();
        Image image;
        int key;
        while(iterator.hasNext()){
            image = iterator.next();
            oridinaryImageMapper.insert(image);
            key = image.getImageID();
            list.add(key);
        }
        return list;
    }

    //通过装有imageID的List集合 查找出imageFile 如果集合为空返回null
    public Map<String,String> selectImageFileByImageID(List<Integer> list){
        if(list.size() == 0 ){
            return null;
        }
        Map<String,String> map = new HashMap<>();
        Iterator<Integer> iterator = list.iterator();
        Integer imageID;
        String imageFile;
        while(iterator.hasNext()){
            imageID = iterator.next();
            imageFile = oridinaryImageMapper.selectImageFileByImageID(imageID);
            //map.put("imageID:"+imageID,imageFile);
            map.put(imageID+"",imageFile);
        }
        return map;
    }

    public Integer insertImage(String title, String location, int author, String content, List<String> imagePathList) {
        //插入图片返回一个match ID
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        List<Integer> imageIdList = new ArrayList<>();
        for(String imagePath : imagePathList ){
            informationMapper.AddImage(title,location,author,imagePath,content,sdf.format(date));
            Integer imageId = informationMapper.foundbyPath(imagePath);
            imageIdList.add(imageId);
        }
        String matchInfo = title+"胁迫信息";
        informationMapper.insertMatch(imageIdList.get(0),matchInfo);
        Integer matchId =informationMapper.foundbyimage(imageIdList.get(0));
        for(Integer i = 1;i<imageIdList.size();i++){
            informationMapper.insertMatchImage(matchId,imageIdList.get(i),matchInfo);
        }
        return matchId;
    }

    public Integer insertVideo(String title, String location, int author, String content, String vedioPath) {
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        informationMapper.insertVideo(title,location,author,vedioPath,content,sdf.format(date));
        Integer VedioId = informationMapper.foundbyvideoPath(vedioPath);
        return VedioId;
    }

}
